package syncAsync;

import java.io.UnsupportedEncodingException;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendCallback;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.common.RemotingHelper;
import com.alibaba.rocketmq.remoting.exception.RemotingException;

public class AsyncProducer {
public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, UnsupportedEncodingException {
	DefaultMQProducer producer=new DefaultMQProducer("asyncProducer");
	producer.setNamesrvAddr("127.0.0.1:9876");
	producer.setInstanceName("asyncProducer-instance");
	producer.setVipChannelEnabled(false);
	producer.start();
	producer.setRetryTimesWhenSendAsyncFailed(0);
	for(int i=0;i<100;i++){
		final int index=1;
		Message msg=new Message("TopicTest","TagA","OrderID188","Hello World".getBytes(RemotingHelper.DEFAULT_CHARSET));
		producer.send(msg,new SendCallback(){

			public void onException(Throwable e) {
				System.out.printf("%-10d Exceptiom %s %n",index,e);
				e.printStackTrace();

			}

			public void onSuccess(SendResult sendResult) {

				System.out.printf("%-10d 0k %s %n",index,sendResult.getMsgId());

			}

		});
	};
	producer.shutdown();
}
}
